﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly BitDbContext _db;
        public UserController(BitDbContext db)
        {
            _db = db;
        }
        #region 增
        [HttpPost("add")]
        public Result Add([FromBody] EM_User user)
        {
            Result result = new Result();
            _db.EM_User.Add(user);
            result.Data = _db.SaveChanges();
            return result;
        }
        #endregion

        #region 删
        [HttpDelete]
        public Result Del(Guid id)
        {
            Result result = new Result();
            EM_User? user = _db.EM_User.Where(w => w.Id == id).SingleOrDefault();
            if (user != null)
                _db.EM_User.Remove(user);
            result.Data = _db.SaveChanges();
            return result;
        }

        [HttpPut("del")]
        public Result IsDel([FromHeader] Guid Id)
        {
            Result result = new Result();
            EM_Merchant? merchant = _db.EM_Merchant.Where(w => w.Id == Id).SingleOrDefault();
            merchant.DelTime = DateTime.Now;
            merchant.IsDel = true;
            result.Data = _db.SaveChanges();
            return result;

        }
        #endregion

        #region 改
        [HttpPut]
        public Result Put([FromBody] EM_User user)
        {
            Result result = new Result();
            _db.EM_User.Update(user);
            result.Data = _db.SaveChanges();
            return result;
        }
        #endregion

        #region 查
        [HttpGet]
        public Result Get()
        {
            Result result = new Result();
            List<EM_User> resultdata = _db.EM_User.ToList();
            result.Data = resultdata;
            result.Code = 200;
            return result;
        }

        [HttpGet("{Id}")]
        public Result GetById(Guid Id)
        {
            Result result = new Result();
            result.Data = _db.EM_User.Where(w => w.Id == Id).SingleOrDefault();
            return result;
        }

        #endregion

        

    }
}
